package com.juzi.wopu.dao;

import java.util.List;

import org.hibernate.SQLQuery;

import com.juzi.wopu.domain.LeaderResponse;
import com.juzi.wopu.query.BaseQuery;
import com.juzi.wopu.query.PageList;

public class LeaderDao extends BaseDao<LeaderResponse>{
	
	@SuppressWarnings("rawtypes")
	@Override
	public PageList findByQuery(final BaseQuery baseQuery){
		SQLQuery query=getSession().createSQLQuery(baseQuery.getCountHql());
		List paramList = baseQuery.getParamList();
		for(int i=0;i<paramList.size();i++){
			query.setParameter(i, paramList.get(i));
		}
		int countLong = (int) query.uniqueResult();
		
		if(countLong==0){
			return new PageList();
		}
		
		int currentPage = baseQuery.getCurrentPage();
		int pageSize = baseQuery.getPageSize();
		
		final PageList pageList=new PageList(currentPage,pageSize,countLong);
		
		SQLQuery dataQuery=getSession().createSQLQuery(baseQuery.getLimitHql()).
				addEntity(LeaderResponse.class);

		for(int i=0;i<paramList.size();i++){
			dataQuery.setParameter(i, paramList.get(i));
		}
		
		int firstResult=(pageList.getCurrentPage()-1) * pageList.getPageSize();
		int maxResule = pageList.getPageSize();
		dataQuery.setFirstResult(firstResult);
		dataQuery.setMaxResults(maxResule);
		List rows=dataQuery.list();

		pageList.setRows(rows);
		return pageList;
	}
}
